package com.ys.nms.module.business.controller.admin.companyProperty;

import com.fhs.core.trans.anno.TransMethodResult;
import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import com.ys.nms.framework.excel.core.util.ExcelUtils;
import com.ys.nms.module.business.controller.admin.companyProperty.vo.CompanyPropertyPageReqVO;
import com.ys.nms.module.business.controller.admin.companyProperty.vo.CompanyPropertyRespVO;
import com.ys.nms.module.business.controller.admin.companyProperty.vo.CompanyPropertySaveReqVO;
import com.ys.nms.module.business.dal.dataobject.companyproperty.CompanyPropertyDO;
import com.ys.nms.module.business.libreApi.devices.req.DeviceAddRequest;
import com.ys.nms.module.business.service.companyproperty.CompanyPropertyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.ys.nms.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 资产")
@RestController
@RequestMapping("/business/company-property")
@Validated
public class CompanyPropertyController {

    @Resource
    private CompanyPropertyService companyPropertyService;

    @PostMapping("/create")
    @Operation(summary = "创建资产")
    @PreAuthorize("@ss.hasPermission('nms:company-property:create')")
    public CommonResult<Integer> createCompanyProperty(@Valid @RequestBody CompanyPropertySaveReqVO createReqVO) {
        return success(companyPropertyService.createCompanyProperty(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资产")
    @PreAuthorize("@ss.hasPermission('nms:company-property:update')")
    public CommonResult<Boolean> updateCompanyProperty(@Valid @RequestBody CompanyPropertySaveReqVO updateReqVO) {
        companyPropertyService.updateCompanyProperty(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateDevice")
    @Operation(summary = "更新资产")
    @PreAuthorize("@ss.hasPermission('nms:company-property:update')")
    public CommonResult<Boolean> updateCompanyProperty(@Valid @RequestBody DeviceAddRequest updateReqVO) {
        companyPropertyService.updateDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资产")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nms:company-property:delete')")
    public CommonResult<Boolean> deleteCompanyProperty(@RequestParam("id") Integer id) {
        companyPropertyService.deleteCompanyProperty(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除资产")
                @PreAuthorize("@ss.hasPermission('nms:company-property:delete')")
    public CommonResult<Boolean> deleteCompanyPropertyList(@RequestParam("ids") List<Integer> ids) {
        companyPropertyService.deleteCompanyPropertyListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资产")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nms:company-property:query')")
    @TransMethodResult
    public CommonResult<CompanyPropertyRespVO> getCompanyProperty(@RequestParam("id") Integer id) {
        CompanyPropertyRespVO companyProperty = companyPropertyService.getCompanyProperty(id);
        return success(companyProperty);
    }

    @GetMapping("/page")
    @Operation(summary = "获得资产分页")
    @PreAuthorize("@ss.hasPermission('nms:company-property:query')")
    @TransMethodResult
    public CommonResult<PageResult<CompanyPropertyRespVO>> getCompanyPropertyPage(@Valid CompanyPropertyPageReqVO pageReqVO) {
        PageResult<CompanyPropertyDO> pageResult = companyPropertyService.getCompanyPropertyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CompanyPropertyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资产 Excel")
    @PreAuthorize("@ss.hasPermission('nms:company-property:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCompanyPropertyExcel(@Valid CompanyPropertyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CompanyPropertyDO> list = companyPropertyService.getCompanyPropertyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资产.xls", "数据", CompanyPropertyRespVO.class,
                        BeanUtils.toBean(list, CompanyPropertyRespVO.class));
    }

}